<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2019/1/23
 * Time: 下午9:28
 */

namespace App\Lib;

use App\Lib\geotransform;


class map
{

    public static $x_PI  = 52.35987755982988;
    public static $PI  = 3.1415926535897932384626;
    public static $a = 6378245.0;
    public static $ee = 0.00669342162296594323;

    private $ak = "gk8ahs0DUX539cRYNu8UBBZlw0kFE16j";
    /**
     * 返回ip经纬度
     * @param $ip
     * @return array
     */
    public function get_location($ip){
        $url = "http://api.map.baidu.com/location/ip?ak=$this->ak&ip=$ip&coor=bd09ll";
        $ret = curl_request($url, null, 'urlencoded', 'GET');
        $data = json_decode($ret, true);

        if($data["status"] != 0){
            return null;
        }
        if (empty($data['content'])){
            return null;
        }
        if (empty($data['content']['point'])){
            return null;
        }

        $geo = geotransform::bd09_To_Gps84($data['content']['point']['y'], $data['content']['point']['x']);
        $ret_data = [
            'lng' => $geo['lon'],
            'lat' => $geo['lat']
        ];


        return $ret_data;
    }


    /**
     * 获取周边位置信息
     * @param $lng
     * @param $lat
     * @return array|null
     */
    public function get_pois($lng, $lat){
        $geo = geotransform::gps84_To_bd09($lat, $lng);
        //http://api.map.baidu.com/geocoder/v2/?&location=40.047013,116.447861&output=json&pois=1&ak=gk8ahs0DUX539cRYNu8UBBZlw0kFE16j
        $geo_lat = $geo['lat'];
        $geo_lng = $geo['lon'];
        $url = "http://api.map.baidu.com/geocoder/v2/?ak=$this->ak&output=json&pois=1&location=$geo_lat,$geo_lng";
        $ret = curl_request($url, null, 'urlencoded', 'GET');
        $data = json_decode($ret, true);
        if ($data["status"] != 0) {
            return null;
        }
        if (empty($data['result'])) {
            return null;
        }
        if (empty($data['result']['pois'])) {
            return null;
        }
        if (empty($data['result']['pois'])) {
            return null;
        }
        $ret_data = [];
        foreach ($data['result']['pois'] as $k => $v) {
            $item = [];
            $geo = geotransform::bd09_To_Gps84($v['point']['y'], $v['point']['x']);
            $item['name'] = $v['name'];
            $item['lng'] = $geo['lon'];
            $item['lat'] = $geo['lat'];
            $item['addr'] = $v['addr'];
            $ret_data[] = $item;

        }
        return $ret_data;

    }


    /**
     * 关键字搜索当前位置
     * @param $word
     * @param $lng
     * @param $lat
     * @return array|null
     */
    public function get_search_pois($word, $lng, $lat){
        $geo = geotransform::gps84_To_bd09($lat, $lng);
        $geo_lat = $geo['lat'];
        $geo_lng = $geo['lon'];
        //http://api.map.baidu.com/geocoder/v2/?&location=40.047013,116.447861&output=json&pois=1&ak=gk8ahs0DUX539cRYNu8UBBZlw0kFE16j
        $word = urlencode($word);
        $url = "http://api.map.baidu.com/place/v2/search?query=$word&location=$geo_lat,$geo_lng&radius=2000&output=json&ak=$this->ak";
        $ret = curl_request($url, null, 'urlencoded', 'GET');
        $data = json_decode($ret, true);
        if($data["status"] != 0){
            return null;
        }
        if (empty($data['results'])){
            return null;
        }

        $ret_data = [];
        foreach ($data['results'] as $k => $v){
            $item = [];
            $geo = geotransform::bd09_To_Gps84($v['location']['lat'], $v['location']['lng']);
            $item['name'] = $v['name'];
            $item['lng'] = $geo['lon'];
            $item['lat'] = $geo['lat'];
            $item['addr'] = $v['address'];
            $ret_data[] = $item;

        }
        return $ret_data;
    }


    public function get_geoconv($lng, $lat){
        $url = "http://api.map.baidu.com/geoconv/v1/?coords=$lng,$lat&from=1&to=5&ak=$this->ak";
        $ret = curl_request($url, null, 'urlencoded', 'GET');
        $data = json_decode($ret, true);
        if($data["status"] != 0){
            return null;
        }
        if (empty($data['result'][0])){
            return null;
        }
        $item = [];
        $item['lng'] = $data['result'][0]['x'];
        $item['lat'] = $data['result'][0]['y'];
        return $item;
    }



}